package com.example.applicationview.viewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 1，测量自身宽高（其中也要测量子控件 的宽高）
 * 2，布局子控件 （根据子控件宽高 摆放子控件）
 */
public class MyViewGroup1_Copy extends ViewGroup {

    public MyViewGroup1_Copy(Context context) {
        this(context, null);
    }

    public MyViewGroup1_Copy(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyViewGroup1_Copy(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**
         * 获得此ViewGroup上级容器为其推荐的宽和高，以及计算模式
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
// 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int width; //最终宽
        int height; //最终高
        int height1 = 0; //第一列高
        int height2 = 0; //第二列高
        int width1 = 0; //第一行宽
        int width2 = 0; //第二行宽
        int childCount = getChildCount(); //子view数量
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            MarginLayoutParams layoutParams = (MarginLayoutParams)child.getLayoutParams();
            //第一行
            if (i == 0 || i == 1) {
                width1 += child.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
            }
            //第二行
            if (i == 2 || i == 3) {
                width2 += child.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
            }
            //第一列
            if (i == 0 || i == 2) {
                height1 += child.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;
            }
            //第一列
            if (i == 1 || i == 3) {
                height2 += child.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;
            }
        }

        width = Math.max(width1, width2);
        height = Math.max(height1, height2);

        setMeasuredDimension((widthMode == MeasureSpec.EXACTLY? sizeWidth: width), (heightMode == MeasureSpec.EXACTLY? sizeHeight: height));

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int cCount = getChildCount();
        int cWidth = 0; //子控件的宽
        int cHeight = 0; //子控件的高
        int pWidth = getWidth(); //父控件的宽
        int pHeight = getHeight(); //父控件的高
        MarginLayoutParams cParams = null;
        /**
         * 遍历所有childView根据其宽和高，以及margin进行布局
         */
        for (int i = 0; i < cCount; i++) {
            View childView = getChildAt(i);
            cWidth = childView.getMeasuredWidth();
            cHeight = childView.getMeasuredHeight();
            cParams = (MarginLayoutParams) childView.getLayoutParams();
            int cl = 0, ct = 0, cr = 0, cb = 0;

            switch (i) {
                case 0:
                    cl = cParams.leftMargin;
                    ct = cParams.topMargin;
                    break;
                case 1:
                    cl = pWidth - cWidth - cParams.leftMargin
                            - cParams.rightMargin;
                    ct = cParams.topMargin;

                    break;
                case 2:
                    cl = cParams.leftMargin;
                    ct = pHeight - cHeight - cParams.bottomMargin;
                    break;
                case 3:
                    cl = pWidth - cWidth - cParams.leftMargin
                            - cParams.rightMargin;
                    ct = pHeight - cHeight - cParams.bottomMargin;
                    break;

            }
            cr = cl + cWidth;
            cb = cHeight + ct;
            childView.layout(cl, ct, cr, cb);
        }

    }


}
